package com.zwd.cases.utils;

import java.io.*;

/**
 * @Description Txt文件操作工具类（恢复mysql的db数据）
 * @Author zhengwd
 * @Date 2022/5/13 17:06
 **/
public class TxtUtil {

    public static void main(String[] args) {

        /*
            db日志数据案例（PS！！！ 改案例整理仅用于学习，具体有应用请自行核查！！！）：
                UPDATE `zwd`.`user`
                WHERE
                @1=1
                @2='001'
                @3='zwd'
                @4='xxx'
                @5='xxx'
                @6='21'
                @7='xxx'
                SET
                @1=1
                @2='002'
                @3='zzz'
                @4='xxxA'
                @5='xxxA'
                @6='27'
                @7='xxxA'

              注解：
                    @1=1   变更前id；
                    @6='21' 变更前age；

             目标输出：
              outStr===>>>>>>
                 UPDATE `zwd`.`user` set    age='21'   where id=1;
                 UPDATE `zwd`.`user` set    age='23'   where id=2;
        */

        // 0.测试用，获取一张项目图片（可不要）
        String contextPath = getContextPath();
        contextPath = contextPath.replace("target/test-classes/", "");

        StringBuffer outStr = new StringBuffer();
        BufferedWriter bw = null;
        // 1.设置输出文件位置
        bw = initOutDataRoot(contextPath, bw);
        // 2.读取txt文件数据
        doWriteData(contextPath, outStr, bw);
    }

    private static BufferedWriter initOutDataRoot(String contextPath, BufferedWriter bw) {
        try {
            // 方式一：直接桌面输出
//            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("C:\\Users\\86178\\Desktop\\zwdOut.txt")),
//                    "UTF-8"));

            // 方式二：生成到项目中：
            String txtFilePath = contextPath + "src/test/java/com/zwd/cases/demo/file/test/zwdOut.txt";
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(txtFilePath)),
                    "UTF-8"));

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return bw;
    }

    private static void doWriteData(String contextPath, StringBuffer outStr, BufferedWriter bw) {
        try {
            // 方式一：直接读取桌面文件
//            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("C:\\Users\\86178\\Desktop\\zwd.txt")),
//                    "UTF-8"));

            // 方式二：读取项目中w文件：
            String txtFilePath = contextPath + "src/test/java/com/zwd/cases/demo/file/test/zwd.txt";
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(txtFilePath)),
                    "UTF-8"));

            String lineTxt;
            StringBuffer idStr = null;
            int count = 1;
            while ((lineTxt = br.readLine()) != null) {
                // 数据以逗号分隔
                StringBuffer str = new StringBuffer();
                // 需要恢复的数据项-主键id
                if (lineTxt.contains("@1=")) {
                    idStr = new StringBuffer(lineTxt.replace("@1=", "where id="));
                    idStr.append("; ");
                    idStr.append("\r\n");
                }
                // 需要恢复的数据项
                if (lineTxt.contains("@6=")) {
                    str = new StringBuffer(" UPDATE `zwd`.`user` set ");
                    str.append(lineTxt.replace("@6=", "age="));
                    str.append(idStr);
                    count++;
                }
                if (str.length() > 0 && count%2 == 0) {
                    outStr.append(str);
                    // 输出数据
                    try {
                        bw.write(String.valueOf(str));
                        bw.newLine();
                    } catch (Exception e) {
                        System.err.println("write errors :" + e);
                    }
                }
            }
            System.out.println("outStr===>>>>>>");
            System.out.println(outStr);
            br.close();
            bw.close();
        } catch (Exception e) {
            System.err.println("read errors :" + e);
        }
    }

    /**
     * 获取上下文路径
     * @return
     */
    private static String getContextPath() {
        return PdfUtil.class.getResource("/").getPath();
    }
}
